package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.erp.dto.ManufactureProceduringDto;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.*;
import com.guigu.erp.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * ClassName:ManufactureProceduringController
 * Package:com.guigu.erp.controller
 * Description:
 *
 * @Date:2024/3/27 18:50
 * @Author:chenchong
 */
@Controller
@RequestMapping("/ManufactureProceduring")
public class ManufactureProceduringController {

    @Autowired
    private ManufactureProcedureService manufactureProcedureService;
    @Autowired
    private ManufactureProceduringService manufactureProceduringService;
    @Autowired
    private ManufactureManufactureService manufactureManufactureService;
    @Autowired
    private ManufactureProcedureModulingService manufactureProcedureModulingService;

    @Autowired
    private ManufactureProcedureModuleService manufactureProcedureModuleService;
    @Autowired
    private StorageGatherDetailsService storageGatherDetailsService;
    @Autowired
    private NumberUtils numberUtils;
    @Autowired
    private StorageGatherService storageGatherService;
    @Autowired
    private DesignFileService designFileService;

    //    工序交接标志
//    G005-0: 未交接
//    G005-1: 已交接
//    G005-2: 已审核
    @ResponseBody
    @RequestMapping("/add")
    public String add(@RequestBody ManufactureProceduringDto dto) {
        //生产总表S002-1: 未审核
        ManufactureManufacture byId1 = manufactureManufactureService.getById(dto.getId());
        byId1.setManufactureProcedureTag("S002-1");
        manufactureManufactureService.updateById(byId1);

        //生产工序表PROCEDURE_FINISH_TAG
        //        G004-0: 未开始
        //        G004-1: 已完成
        //        G004-2: 未完成
        //        G004-3: 已审核
        if (dto.getHao().equals("完成")) {
            ManufactureProcedure byId = manufactureProcedureService.getById(dto.getId1());
            byId.setProcedureFinishTag("G004-1");
            manufactureProcedureService.updateById(byId);
        } else {
            ManufactureProcedure byId = manufactureProcedureService.getById(dto.getId1());
            byId.setProcedureFinishTag("G004-2");
            manufactureProcedureService.updateById(byId);
        }
        //添加生产工序过程记录
        ManufactureProcedure byId = manufactureProcedureService.getById(dto.getId1());
        ManufactureProceduring man = new ManufactureProceduring();
        man.setParentId(dto.getId());
        man.setDetailsNumber(byId.getDetailsNumber());
        man.setProcedureId(byId.getProcedureId());
        man.setProcedureName(byId.getProcedureName());
        man.setLabourHourAmount(dto.getLabourHourAmount());
        man.setCostPrice(byId.getCostPrice());
        man.setSubtotal(byId.getSubtotal());
        man.setRegCount(new BigDecimal(1));
        man.setProcedureResponsiblePerson(dto.getProcedureResponsiblePerson());
        man.setRegister(dto.getRegister());
        man.setRegisterTime(dto.getRegisterTime());
        manufactureProceduringService.save(man);
//添加生产工序物料过程记录
        List<ManufactureProcedureModule> details = dto.getDetails();
        for (int i = 0; i < details.size(); i++) {
            ManufactureProcedureModule storageGatherDetails = details.get(i);
            QueryWrapper<ManufactureProcedureModule> qw3 = new QueryWrapper<>();
            qw3.eq("PARENT_ID", dto.getId1());
            List<ManufactureProcedureModule> list = manufactureProcedureModuleService.list(qw3);
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getProductId().equals(details.get(i).getProductId())) {
                    list.get(j).setRealAmount(list.get(j).getRealAmount().add(storageGatherDetails.getRealAmount()));
                    manufactureProcedureModuleService.updateById(list.get(j));
                }

            }

            ManufactureProcedureModuling man1 = new ManufactureProcedureModuling();
            man1.setId(null);
            man1.setParentId(man.getId());
            man1.setDetailsNumber(i + 1);
            man1.setProductId(storageGatherDetails.getProductId());
            man1.setProductName(storageGatherDetails.getProductName());
            man1.setCostPrice(storageGatherDetails.getCostPrice());
            man1.setAmount(storageGatherDetails.getRealAmount());
            man1.setSubtotal(storageGatherDetails.getRealAmount().multiply(storageGatherDetails.getCostPrice()));
            manufactureProcedureModulingService.save(man1);
        }
        return "OK";
    }

    @ResponseBody
    @RequestMapping("/xiu1")
    public String xiu1(int id, int id1) {
        //生产总表S002-0:
        ManufactureManufacture byId1 = manufactureManufactureService.getById(id);
        byId1.setManufactureProcedureTag("S002-0");
        manufactureManufactureService.updateById(byId1);

        QueryWrapper<ManufactureProcedureModule> qw = new QueryWrapper<>();
        qw.eq("PARENT_ID", id1);
        BigDecimal b = new BigDecimal(0);
        BigDecimal b1 = new BigDecimal(0);
        List<ManufactureProcedureModule> list = manufactureProcedureModuleService.list(qw);
        for (int j = 0; j < list.size(); j++) {
            ManufactureProcedureModule manufactureProcedureModule = list.get(j);
            BigDecimal amount = manufactureProcedureModule.getAmount();
            b = b.add(amount);
            BigDecimal amount1 = manufactureProcedureModule.getRealAmount();
            b1 = b1.add(amount1);
        }
        if (b.equals(b1)) {
            ManufactureProcedure byId = manufactureProcedureService.getById(id1);
            byId.setProcedureFinishTag("G004-3");
            byId.setProcedureTransferTag("G005-1");
            manufactureProcedureService.updateById(byId);
        } else {
            ManufactureProcedure byId = manufactureProcedureService.getById(id1);
            byId.setProcedureFinishTag("G004-0");
            manufactureProcedureService.updateById(byId);
        }

        return "ok";
    }

    @ResponseBody
    @RequestMapping("/updateById")
    public Boolean updateById(int id1, int textnum, int id) {
        //生产总表S002-1: 未审核
        ManufactureManufacture byId1 = manufactureManufactureService.getById(id);
        byId1.setManufactureProcedureTag("S002-1");
        manufactureManufactureService.updateById(byId1);

        ManufactureProcedure byId = manufactureProcedureService.getById(id1);
        byId.setRealAmount(new BigDecimal(textnum));

        boolean b = manufactureProcedureService.updateById(byId);
        return b;
    }

    @ResponseBody
    @RequestMapping("/updateById2")
    public Boolean updateById2(int id1, int id) {
        //生产总表S002-1: 未审核
        ManufactureManufacture byId1 = manufactureManufactureService.getById(id);
        byId1.setManufactureProcedureTag("S002-0");
        manufactureManufactureService.updateById(byId1);
        System.out.println("byId1"+byId1);
        ManufactureProcedure byId = manufactureProcedureService.getById(id1);
        byId.setProcedureTransferTag("G005-2");
        byId.setRealModuleSubtotal(byId.getModuleSubtotal());
        manufactureProcedureService.updateById(byId);


        QueryWrapper<ManufactureProcedure> qw2 = new QueryWrapper<>();
        qw2.eq("PARENT_ID", id);
        List<ManufactureProcedure> list = manufactureProcedureService.list(qw2);
        int w1 = 0;
        for (int i = 0; i < list.size(); i++) {
            ++w1;
        }
        int w = 0;
        for (int i = 0; i < list.size(); i++) {
            ManufactureProcedure mana = list.get(i);
            if (mana.getProcedureTransferTag().equals("G005-2")) {
                ++w;
            }
        }
        Boolean b1 = false;
        if (w == w1) {
            ManufactureManufacture byId2 = manufactureManufactureService.getById(id);
            byId2.setManufactureProcedureTag("S002-2");
          byId2.setTestedAmount(byId2.getAmount());
            b1 = manufactureManufactureService.updateById(byId2);
            System.out.println("byId2"+byId2);
            QueryWrapper<DesignFile> qw=new QueryWrapper<DesignFile>();
            qw.eq("PRODUCT_ID", byId2.getProductId());
            DesignFile one = designFileService.getOne(qw);

            StorageGather stor = new StorageGather();
            String typeNumber = numberUtils.getTypeNumber("401");
            stor.setGatherId(typeNumber);
            stor.setReason("R001-1");
            stor.setAmountSum(byId2.getAmount());
            stor.setCostPriceSum(byId2.getAmount().multiply(one.getCostPrice()));
            BigDecimal num = new BigDecimal(0);
            stor.setGatheredAmountSum(num);
            stor.setRegisterTime(new Date());
            stor.setCheckTime(new Date());
            stor.setCheckTag("S001-1");
            stor.setStoreTag("K002-1");
            storageGatherService.save(stor);

            StorageGatherDetails Details=new StorageGatherDetails();
            Details.setParentId(stor.getId());
            Details.setProductId(byId2.getProductId());
            Details.setProductName(byId2.getProductName());
            Details.setAmount(byId2.getAmount());
            Details.setCostPrice(one.getRealCostPrice());
            Details.setSubtotal(byId2.getAmount().multiply(one.getCostPrice()));
            Details.setGatherTag("K002-1");
            storageGatherDetailsService.save(Details);

        }
        return b1;
    }

    @ResponseBody
    @RequestMapping("/updateById10")
    public Boolean updateById10(int id){

        ManufactureProcedure man=  manufactureProcedureService.getById(id);
       int hao= man.getDetailsNumber();
        if(hao != 1){
            int hao1=hao-1;
            QueryWrapper<ManufactureProcedure> qw=new QueryWrapper<>();
            qw.eq("PARENT_ID",man.getParentId());
            qw.eq("DETAILS_NUMBER",hao1);
            ManufactureProcedure   byId=manufactureProcedureService.getOne(qw);
            Boolean b=false;
            if(byId.getProcedureTransferTag().equals("G005-2")){
                b=true;
            }
            return b;
        }

        return true;
    }

}
